﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class roomstatus
    Inherits cacheControlProcessor

    Public Const _pathidentity As String = network.pathSeperator + "room"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function normalRetiredTicks() As Long
        Return secondsToTicks(10)
    End Function

    Public Shared Function generatelink(ByVal host As String, ByVal port As String) As String
        Dim rtn As String = Nothing
        rtn = strmid(_pathidentity, strlen(network.pathSeperator))
        rtn = urlUtils.appendQuery(rtn, query.host, host, query.port, port)
        Return rtn
    End Function

    Public Shared Function generatelink(ByVal host As String, ByVal port As Int32) As String
        Return generatelink(host, Convert.ToString(port))
    End Function

    Private Delegate Function boolcall() As Boolean
    Private Delegate Function uint64call(ByRef sz As UInt64) As Boolean
    Private Delegate Function stringcall() As String

    Private Function createBoolStatus(ByVal ctr As xmlcontrol, ByVal s As String, ByVal c As boolcall) As xmlcontrol
        assert(Not ctr Is Nothing, "ctr is nothing.")
        assert(Not c Is Nothing, "boolcall is nothing.")
        ctr.text = s
        If c() Then
            ctr.text += httpServer2.currentContext().string(stringId.ROOM_STATUS_YES)
        Else
            ctr.text += httpServer2.currentContext().string(stringId.ROOM_STATUS_NO)
        End If

        Return ctr
    End Function

    Private Function createUInt64Status(ByVal ctr As xmlcontrol, ByVal s As String, ByVal c As uint64call, _
                                        Optional ByVal memoryShow As Boolean = False) As xmlcontrol
        assert(Not ctr Is Nothing, "ctr is nothing.")
        assert(Not c Is Nothing, "boolcall is nothing.")
        ctr.text = s
        Dim sz As UInt64 = Nothing
        If c(sz) Then
            If memoryShow Then
                ctr.text += memorySizeDisplay(sz)
            Else
                ctr.text += Convert.ToString(sz)
            End If
        Else
            ctr.text += httpServer2.currentContext().string(stringId.TALK_FAILED)
        End If

        Return ctr
    End Function

    Private Function createStringStatus(ByVal ctr As xmlcontrol, ByVal s As String, ByVal c As stringcall) As xmlcontrol
        assert(Not ctr Is Nothing, "ctr is nothing.")
        assert(Not c Is Nothing, "boolcall is nothing.")
        ctr.text = s + c()

        Return ctr
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        Dim page As rvpage = Nothing
        page = context.createControl(New rvpage())
        page.addcsslink(cssrequest.generateCssfileLinkUrl("roomstatus.css"))
        page.title().text = context.string(stringId.ROOMSTATUS_TITLE)

        Dim host As String = Nothing
        Dim port As Int32 = Nothing
        host = context.url().query(query.host)
        port = context.url().queryInt32(query.port)

        If isemptystring(host) OrElse port = npos Then
            Dim div As div = Nothing
            div = context.createControl(New div())
            div.text = context.string(stringId.NO_HOST_OR_PORT)
            page.leftrail().addchild(div)
        Else
            Dim talk As storoomTalk = Nothing
            talk = roomview.talk(host, port)
            Dim ul As ul = Nothing
            ul = context.createControl(New ul())
            page.leftrail().addchild(ul)

            Dim li As li = Nothing
            li = context.createControl(New colorli(page))
            li.text = context.string(stringId.TARGET_STOROOM) + displayHostPort(host, port)
            ul.addchild(li)

            If talk.health() Then
                ul.addchild(createStringStatus(context.createControl(New colorli(page)), _
                                               context.string(stringId.ROOM_STATUS_VERSION), _
                                               AddressOf talk.version))

                ul.addchild(createBoolStatus(context.createControl(New colorli(page)), _
                                             context.string(stringId.ROOM_STATUS_HEALTH), _
                                             AddressOf talk.health))
                ul.addchild(createUInt64Status(context.createControl(New colorli(page)), _
                                               context.string(stringId.ROOM_STATUS_KEYCOUNT), _
                                               AddressOf talk.keycount))
                ul.addchild(createBoolStatus(context.createControl(New colorli(page)), _
                                             context.string(stringId.ROOM_STATUS_FULL), _
                                             AddressOf talk.full))
                ul.addchild(createBoolStatus(context.createControl(New colorli(page)), _
                                             context.string(stringId.ROOM_STATUS_EMPTY), _
                                             AddressOf talk.empty))
                ul.addchild(createUInt64Status(context.createControl(New colorli(page)), _
                                               context.string(stringId.ROOM_STATUS_CAPACITY), _
                                               AddressOf talk.capacity, True))
                ul.addchild(createUInt64Status(context.createControl(New colorli(page)), _
                                               context.string(stringId.ROOM_STATUS_VALUESIZE), _
                                               AddressOf talk.valuesize, True))

                li = context.createControl(New colorli(page))
                li.text = context.string(stringId.ROOM_STATUS_STATUS)
                Dim allslice As UInt64
                Dim unhealthSlice As UInt64
                If Not talk.status(allslice, unhealthSlice) Then
                    li.text += context.string(stringId.TALK_FAILED)
                Else
                    Dim healthSlice As UInt64 = Nothing
                    Try
                        healthSlice = allslice - unhealthSlice
                    Catch ex As Exception
                        raiseError("caught allslice < unhealthSlice, ignore healthSlice.", errorHandle.errorType.warning)
                    End Try
                    li.text += context.string(stringId.ROOM_STATUS_STATUS_ALLSLICE) + _
                                   Convert.ToString(allslice) + " - " + _
                               context.string(stringId.ROOM_STATUS_STATUS_HEALTHSLICE) + _
                                   Convert.ToString(healthSlice) + " - " + _
                               context.string(stringId.ROOM_STATUS_STATUS_UNHEALTHSLICE) + _
                                   Convert.ToString(unhealthSlice)
                End If
                ul.addchild(li)

                li = context.createControl(New colorli(page))
                Dim a As a = Nothing
                a = context.createControl(New a())
                a.text = context.string(stringId.TO_LIST_VIEW)
                a.href = listview.generatelink(host, port)
                li.addchild(a)
                ul.addchild(li)
            Else
                li = context.createControl(New colorli(page))
                li.text = context.string(stringId.TALK_FAILED)
                ul.addchild(li)
            End If
        End If

        control = page
        Return True
    End Function
End Class
